<?php

/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/15
 * Time: 下午4:40
 */

namespace app\src\accountScore\logic;

use app\src\accountScore\model\AccountScore;
use app\src\base\logic\BaseLogic;

class AccountScoreLogic extends BaseLogic
{

    const SCORE_TYPE_1 = 1; //可提现积分
    const SCORE_TYPE_2 = 2; //不可提现积分

    protected function _init()
    {
        $this->setModel(new AccountScore());
    }

    /**
     * 创建用户或平台积分账户
     * @param $type
     * @param $account_id
     * @return array
     */
    protected function create($type, $account_id)
    {
        $data = [
            'type' => $type,
            'score1' => 0,
            'score2' => 0,
            'account_id' => $account_id,
            'status' => 0
        ];

        $result = $this->add($data);

        if($result['status']){
            return $this->apiReturnSuc($data);
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 获取用户或平台积分
     * @param $type
     * @param $account_id
     * @return array
     */
    public function getScore($type, $account_id)
    {
        //判断是否需要创建
        $map = [
            'type' => $type,
            'account_id' => $account_id,
            'status' => 0
        ];
        $result = $this->getInfo($map);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if(!empty($result['info'])){
            $info = $result['info'];
        }else{
            $result = $this->create($type, $account_id);
            if(!$result['status']) return $this->apiReturnErr($result['info']);
            $info = $result['info'];
        }

        $account_score_info = [
            'type' => $info['type'],
            'score1' => $info['score1'],
            'score2' => $info['score2'],
            'account_id' => $info['account_id']
        ];

        return $this->apiReturnSuc($account_score_info);

    }

    /**
     * 增加积分
     * @param $type
     * @param $account_id
     * @param $score_type
     * @param $change
     * @return array
     */
    public function plusScore($type, $account_id, $score_type, $change, $reason = '')
    {
        if($change <= 0){
            return $this->apiReturnErr('change must bigger then 0');
        }

        $result = $this->getScore($type, $account_id);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if(!in_array($score_type, [self::SCORE_TYPE_1, self::SCORE_TYPE_2])){
            return $this->apiReturnErr('error score_type');
        }
        $before_score = $result['info'];
        $map = [
            'type' => $type,
            'account_id' => $account_id
        ];

        $data = [];
        $after = 0;
        $before = 0;
        if($score_type == self::SCORE_TYPE_1){
            $before = intval($before_score['score1']);
            $data['score1'] = intval($before_score['score1']) + intval($change);
            $after = $data['score1'];
        }
        if($score_type == self::SCORE_TYPE_2){
            $before = intval($before_score['score2']);
            $data['score2'] = intval($before_score['score2']) + intval($change);
            $after = $data['score2'];
        }
        $result = $this->save($map, $data);

        if($result['status']){
            $result = (new AccountScoreHistoryLogic)->addHistory($type, $account_id, $score_type, AccountScoreHistoryLogic::SCORE_PLUS, $before, $after, $reason);
            if(!$result['status']) return $this->apiReturnErr($result['info']);
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 减少积分
     * @param $type
     * @param $account_id
     * @param $score_type
     * @param $change
     * @return array
     */
    public function minusScore($type, $account_id, $score_type, $change, $reason = '')
    {
        if($change <= 0){
            return $this->apiReturnErr('change must bigger then 0');
        }

        $result = $this->getScore($type, $account_id);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if(!in_array($score_type, [self::SCORE_TYPE_1, self::SCORE_TYPE_2])){
            return $this->apiReturnErr('error score_type');
        }
        $before_score = $result['info'];
        $map = [
            'type' => $type,
            'account_id' => $account_id
        ];

        $data = [];
        $before = 0;
        $after = 0;

        if($score_type == self::SCORE_TYPE_1){
            if(intval($before_score['score1']) - intval($change) < 0){
                //操作后小于0报错
                return $this->apiReturnErr('error, after score less then 0');
            }
            $before = intval($before_score['score1']);
            $data['score1'] = intval($before_score['score1']) - intval($change);
            $after = $data['score1'];
        }
        if($score_type == self::SCORE_TYPE_2){
            if(intval($before_score['score2']) - intval($change) < 0){
                //操作后小于0报错
                return $this->apiReturnErr('error, after score less then 0');
            }
            $before = intval($before_score['score2']);
            $data['score2'] = intval($before_score['score2']) - intval($change);
            $after = $data['score2'];
        }
        $result = $this->save($map, $data);

        if($result['status']){
            (new AccountScoreHistoryLogic)->addHistory($type, $account_id, $score_type, AccountScoreHistoryLogic::SCORE_MINUS, $before, $after, $reason);
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }

    /**
     * 设置用户或平台积分
     * @param $type
     * @param $account_id
     * @param array $score
     * @param string $reason
     * @return array
     */
    public function setScore($type, $account_id, $score = ['score1' => 0, 'score2' => 0], $reason = '')
    {

        $result = $this->getScore($type, $account_id);
        if(!$result['status']) return $this->apiReturnErr($result['info']);

        $before_info = $result['info'];

        $map = [
            'type' => $type,
            'account_id' => $account_id
        ];

        $data = [
            'score1' => $score['score1'],
            'score2' => $score['score2']
        ];

        $result = $this->save($map, $data);
        if($result['status']){
            if($before_info['score1'] != $score['score1']){
                $before = $before_info['score1'];
                $after = $score['score1'];
                (new AccountScoreHistoryLogic)->addHistory($type, $account_id, self::SCORE_TYPE_1, AccountScoreHistoryLogic::SCORE_SET, $before, $after, $reason);
            }
            if($before_info['score2'] != $score['score2']){
                $before = $before_info['score2'];
                $after = $score['score1'];
                (new AccountScoreHistoryLogic)->addHistory($type, $account_id, self::SCORE_TYPE_2, AccountScoreHistoryLogic::SCORE_SET, $before, $after, $reason);
            }
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }
    }
}